<?php
  
/**
 * acesso a dados
 * 
 * @package 	db
 * @author 		Bruno Maciel <bruno_janga@hotmail.com>, <contato@brunomaciel.com>
 * @copyright	Copyright (C) 2009 NUTES
 * @license		http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @version 	$Id: 06/01/2010
 * 
 */
class postgresql{
	
	var $DB_SERVER_HOST		=	"";
	var $DB_SERVER_USER		=	"";	
	var $DB_SERVER_PASSWORD	=	"";		
	var $DB_SERVER_DATABASE	=	"";
	var $DB_SERVER_PORT		=	5432;
	var $last_insert_id=0;
	
	/**
	 * ultimo insert
	 * 
	 * @author Bruno Maciel<bruno_janga@hotmail.com>
	 * @version $Id 1.0 2013/03/08
	 * 
	 * 
	 * @return array
	 */	
	function getLastInsertId()
	{
		
		return $this->last_insert_id;
			
	}
	
	
	/**
	 * Construtor da classe
	 * 
	 * @author Bruno Maciel<bruno_janga@hotmail.com>
	 * @version $Id 1.0 2010/01/06
	 * 
	 * @param string $host
	 * @param string $user
	 * @param string $pws
	 * @param string $db
	 * 
	 * @return void
	 */	
	function setParameters($host="localhost", $port="5432",
								$user="root",$pws="",$dbname="")
	{

		$this->DB_SERVER_HOST		=	$host;
		$this->DB_SERVER_PORT		=	$port;
		$this->DB_SERVER_USER		=	$user;
		$this->DB_SERVER_PASSWORD	=	$pws;
		$this->DB_SERVER_DATABASE	=	$dbname;
				
	}
	
	
	/**
	 * Responsavel por execultar as instrucoes SQL
	 * 
	 * @author Bruno Maciel<bruno_janga@hotmail.com>
	 * @version $Id 1.0 2010/01/06
	 * 
	 * @param string $sql
	 * 
	 * @return array
	 */	
	function setQuery($sql="")
	{
		//$result		=	array();
		//$array_item	=	array();
		
		//$con = mysql_connect("db-server:3307","star","st@r");
		//mysql_select_db("site_nutes_joomla15",$con);
		
		$string_connect = "host=".$this->DB_SERVER_HOST
								." port=".$this->DB_SERVER_PORT
								." dbname=".$this->DB_SERVER_DATABASE
								." user=".$this->DB_SERVER_USER 
								." password=".$this->DB_SERVER_PASSWORD
								." connect_timeout=5";
		
		try {						
			
			$con_ = @pg_connect( $string_connect );// or die('connection failed');
		//mysql_select_db($this->DB_SERVER_DATABASE,$con_);		
		
		//mysql_query("SET NAMES 'utf8'");
		//mysql_query('SET character_set_connection=utf8');
		//mysql_query('SET character_set_client=utf8');
		//mysql_query('SET character_set_results=utf8');
			if (!$con_) {
			    //die('Não foi possível conectar: '  . pg_last_error($con_));
				throw new AppException( 'Não foi possível conectar: ');
				
			}else{
		
        						
				$query_result = pg_query($con_, $sql);
			
				$this->last_insert_id	=	pg_last_oid( $query_result );
		
				pg_close( $con_ );
	
				$result=true;
			
			}
			
	    }
	    catch (AppException $e) {
	    	$result=false;
	    }
	
		
		return $result;
			
	}
	
	
		
	/**
	 * Responsavel por execultar as instrucoes SQL
	 * 
	 * @author Bruno Maciel<bruno_janga@hotmail.com>
	 * @version $Id 1.0 2010/01/06
	 * 
	 * @param string $sql
	 * 
	 * @return array
	 */	
	function getQuery($sql="")
	{
		$result		=	array();
		$array_item	=	array();
		
		//$con = mysql_connect("db-server:3307","star","st@r");
		//mysql_select_db("site_nutes_joomla15",$con);
		
		$string_connect = "host=".$this->DB_SERVER_HOST
								." port=".$this->DB_SERVER_PORT
								." dbname=".$this->DB_SERVER_DATABASE
								." user=".$this->DB_SERVER_USER 
								." password=".$this->DB_SERVER_PASSWORD
								." connect_timeout=5";
		
		try {						
			
			$con_ = @pg_connect( $string_connect );// or die('connection failed');
			
			
			
			if (!$con_) {
			    //die('Não foi possível conectar: '  . pg_last_error($con_));
				throw new AppException( 'Não foi possível conectar: ' );
				
			}else{
				
				$query_result	=	pg_query( $con_,$sql );
				
			
				while($row = pg_fetch_object($query_result)){
				
				//$array_item=	array();
				//foreach($row as $key=>$item){
				//	$array_ = array(["$key"]=>$item);
				//	array_push($array_item,$array_);			
				//}
				//array_push($result,$array_item);
					array_push($result,$row);
				}
				
				pg_free_result( $query_result );
			
			}
						
		
			pg_close( $con_ );
		
		
		
		}
	    catch (AppException $e) {
	    	$result=false;
	    }
	
	
				
		return $result;
			
	}
	
	
 
	/**
	 * Construtor da classe
	 * 
	 * @author Bruno Maciel<bruno_janga@hotmail.com>
	 * @version $Id 1.0 2010/01/06
	 * 
	 * @param string $host
	 * @param string $user
	 * @param string $pws
	 * @param string $db
	 * 
	 * @return void
	 */	
	function __construct($host="localhost", $port="5432",
								$user="root",$pws="",$db="")
	{

		$this->DB_SERVER_HOST		=	$host;
		$this->DB_SERVER_PORT		=	$port;
		$this->DB_SERVER_USER		=	$user;
		$this->DB_SERVER_PASSWORD	=	$pws;
		$this->DB_SERVER_DATABASE	=	$db;
				
	}
	
		
}

?>
